import request from '@/utils/request'
import { getLocaChannels, hasToken, setLocaChannels } from '@/utils/storage'

export const setUserChannels = (channels) => ({
  type: 'home/userChannels',
  payload: channels,
})

export const setAllChannels = (channels) => ({
  type: 'home/allChannels',
  payload: channels,
})

export const setArticleList = ({ channelId, list, timestamp }) => ({
  type: 'home/setArticleList',
  payload: { channelId, list, timestamp },
})

export const setMoreAction = ({ visible, articleId }) => ({
  type: 'home/setMoreAction',
  payload: { visible, articleId },
})

/**
 * 获取我的频道
 * @returns
 */
export const getUserChannels = () => {
  return async (dispatch) => {
    if (hasToken()) {
      const res = await request.get('/user/channels')
      dispatch(setUserChannels(res.data.channels))
    } else {
      const channels = getLocaChannels()
      if (channels) {
        dispatch(setUserChannels(channels))
      } else {
        const res = await request.get('/user/channels')
        dispatch(setUserChannels(res.data.channels))
        setLocaChannels(res.data.channels)
      }
    }
  }
}

/**
 * 获取所有频道
 */
export const getAllChannels = () => {
  return async (dispatch) => {
    const res = await request.get('/channels')
    dispatch(setAllChannels(res.data.channels))
  }
}

/**
 * 添加
 * @param {*} channel
 * @returns
 */
export const addChannel = (channel) => {
  return async (dispatch, getState) => {
    const userChannels = getState().home.userChannels
    // 区分：游客 和 已登录
    if (hasToken()) {
      // 发送请求进行添加操作
      await request.patch('/user/channels', {
        channels: [channel],
      })
    } else {
      // 需要对本地数据 进行追加
      setLocaChannels([...userChannels, channel])
    }
    // 更新redux的数据
    dispatch(setUserChannels([...userChannels, channel]))
  }
}

/**
 * 删除频道
 * @param {*} channel 频道信息
 * @returns
 */
export const delChannel = (channel) => {
  return async (dispatch, getState) => {
    const { userChannels } = getState().home
    if (hasToken()) {
      await request.delete(`/user/channels/${channel.id}`)
    } else {
      setLocaChannels(userChannels.filter((item) => item.id !== channel.id))
    }
    dispatch(
      setUserChannels(userChannels.filter((item) => item.id !== channel.id))
    )
  }
}

/**
 * 获取文章列表
 * @param {*} channelId 频道id
 * @param {*} timestamp 时间戳
 * @returns
 */
export const getArticleList = (channelId, timestamp, isGetMore) => {
  return async (dispatch, getState) => {
    const res = await request.get('/articles', {
      params: {
        channel_id: channelId,
        timestamp: timestamp,
      },
    })
    const currentObj = getState().home.articles[channelId]
    const list = currentObj?.list
    if (isGetMore) {
      dispatch(
        setArticleList({
          channelId,
          list: [...list, ...res.data.results],
          timestamp: res.data.pre_timestamp,
        })
      )
    } else {
      dispatch(
        setArticleList({
          channelId,
          list: res.data.results,
          timestamp: res.data.pre_timestamp,
        })
      )
    }
  }
}

/**
 * 删除不喜欢的文章
 * @param {*} articleId 文章id
 * @returns
 */
export const unLike = (articleId, activeChannelId) => {
  return async (dispatch, getState) => {
    await request.post('/article/dislikes', {
      target: articleId,
    })
    const currentObj = getState().home.articles[activeChannelId]
    // const arr = currentObj.list.map((item) => item.art_id !== articleId)
    // console.log(arr)
    dispatch(
      setArticleList({
        channelId: activeChannelId,
        list: currentObj.list.filter((item) => item.art_id !== articleId),
        timestamp: currentObj.currentObj,
      })
    )
  }
}
